<table class="list">
<thead>
	<tr>
	<? foreach($header as $key=>$label): ?>
	<?
		$dir = $sort["direction"];
		if($key==$sort["field"]){
			$dir = strtolower($dir)=="asc" ? "desc" : "asc";
		}
		$sortlink = sprintf($pagination["link"], $pagination["page"], $key, $dir);
	?>
		<th<? if($key==$sort["field"]): ?> class="sorted <?= $sort["direction"]; ?>"<? endif; ?>>
			<a href="<?=$sortlink ?>">
			<?=$label; ?>
			</a>
		</th>
	<? endforeach; ?>

	<? if(!empty($actions)): ?>
	<th class="actions"><a href="<?= $actions["create"] ?>" class="action new" title="Kreiraj novi..."></a></th>
	<? endif; ?>

	</tr>
</thead>
<tbody>
	<? foreach($data as $row): ?>
	<tr class="<?=cycle("even","odd"); ?>">
		<? foreach($header as $key=>$label): ?>
		
		<?
			$value = $row[$key];
			if(strpos($key,".")){
				$key_items = explode(".", $key);
				$toeval = '$value = $row';
				foreach($key_items as $ki){
					$toeval .= "[\"$ki\"]";
				}
				eval($toeval.";");
			}
		?>
		
		<td class="<?=$type[$key] ?>"><?= empty($type[$key]) ? $value : format($value, $type[$key]); ?></td>
		<? endforeach; ?>
		<? if(!empty($actions)): ?>
		<td class="actions">
			<a href="<?= sprintf($actions["edit"], $row["id"]); ?>" class="action edit" title="Izmeni..."></a>
			<a href="<?= sprintf($actions["delete"], $row["id"]); ?>" class="action delete" onclick="return confirm('Da li želite da obrišete?')" title="Obriši..."></a>
		</td>
		<? endif; ?>
	</tr>
	<? endforeach; ?>
</tbody>
</table>

<?

	$total = $pagination["total"];
	$per_page = $pagination["per_page"];
	$link = sprintf($pagination["link"], "%s", $sort["field"], $sort["direction"]);
?>
<? if($total > $per_page): ?>
	<?
		$pages = ceil($pagination["total"]/$pagination["per_page"]);
		$n = 5;

		$start = $page - $n; $start = $start<1 ? 1 : $start;
		$end = $start+(2*$n)-1;
		if($end>$pages){
			$end = $pages;
			$start = $end-(2*$n)+1;
		}
	?>

	<div class="pagination">
	<? if($start>1): ?>
		<a href="<?=sprintf($link, 1); ?>">1</a> ...
	<? endif; ?>
	<? for($i=$start; $i<=$end; $i++): ?>
		<? if($i==$pagination["page"]): ?>
			<strong><?= $i; ?></strong>
		<? else: ?>
			<a href="<?=sprintf($link, $i) ?>"><?= $i ?></a>
		<? endif; ?>
	<? endfor; ?>
	<? if($end<$pages): ?>
		 ... <a href="<?=sprintf($link, $pages); ?>"><?=$pages ?></a>
	<? endif; ?>
	</div>
<? endif; ?>